<template>
  <div>
    <!-- 三级分类 -->
    <Categrary :scene="scene" />
    <el-card style="margin: 10px 0;">
      <!-- 场景0 -->
      <div v-show="scene === 0">
        <!-- 没有id禁用 -->
        <el-button icon="Plus" type="primary" :disabled="!categoryStore.c3Id" @click="addSpu">添加SPU</el-button>
        <el-table style="margin: 10px 0;" border="true" :data="records">
          <el-table-column label="序号" type="index" width="100" align="center" :index="formatIndex()" />
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column label="SPU描述" prop="description" show-overflow-tooltip></el-table-column>
          <el-table-column label="SPU操作" align="center">
            <!-- @vue-ignore -->
            <template #="{ row, $index }">
              <el-button type="primary" size="small" icon="Plus" title="添加spu" @click="addSku(row)"></el-button>
              <el-button type="warning" size="small" icon="Edit" title="修改spu" @click="updateSpu(row)"></el-button>
              <el-button type="info" size="small" icon="View" title="查看spu" @click="findSku(row)"></el-button>
              <el-button style="margin-left: 50px;" type="danger" size="default" icon="Delete" title="删除spu"
                @click="delSpu(row)"></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination v-model:current-page="pageNo" v-model:page-size="pageSize" @current-change="handleCurrentChange"
          @size-change="handleSizeChange" :page-sizes="[5, 10, 30, 50]" background
          layout="prev, pager, next, jumper,->,sizes,total " :total="total" v-if="total > 0" />
      </div>
      <!-- 添加、修改spu组件 -->
      <SpuForm v-show="scene === 1" @changeScene="changeScene" ref="spuForm" @getHasSpu="getHasSpu" />
      <!-- 添加sku组件 -->
      <SkuForm v-show="scene === 2" @changeScene="changeScene" ref="skuForm" />
      <!-- el-dialog -->
      <el-dialog title="SKU列表" v-model="dialogVisible" width="50%">
        <el-table border :data="skuArr">
          <el-table-column label="SKU名字" prop="skuName"></el-table-column>
          <el-table-column label="SKU价格" prop="price"></el-table-column>
          <el-table-column label="SKU重量" prop="weight"></el-table-column>
          <el-table-column label="SKU图片">
            <template #="{ row }">
              <img :src="row.skuDefaultImg" :alt="row.skuName" style="width: 100px;height: 100px;">
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, watch, onUnmounted } from 'vue'
// 引入分类的仓库
import useCategoraryStore from '@/store/modules/category';
const categoryStore = useCategoraryStore()
import { reqHasSpu, reqSkuList, reqDelSpu } from '@/api/product/spu/index'
import { ElMessage } from 'element-plus';
import { HasSpuResponseData, Records, SpuData, SkuInfoData, SkuData } from '@/api/product/spu/types'

// 引入子组件
import SpuForm from './spuForm.vue'
import SkuForm from './skuForm.vue'
// 场景值切换
const scene = ref<number>(0) //0:显示已有的spu  1:添加或者修改已有的spu  2：添加sku的结构
// 当前页码
const pageNo = ref<number>(1)
// dialog是否显示
const dialogVisible = ref<boolean>(false)
// 页面大小
const pageSize = ref<number>(5)
// 存储已有spu数据
const records = ref<Records>([])
// 总数
const total = ref<number>(0)
// 存储全部的SKU数据
const skuArr = ref<SkuData[]>([])
// 获取子组件实例
const spuForm = ref()
const skuForm = ref()
// 格式化序号
const formatIndex = () => {
  return (pageNo.value - 1) * pageSize.value + 1
}
// 页面容量发生变化
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getHasSpu()
}
// 页码发生变化
const handleCurrentChange = (val: number) => {
  pageNo.value = val
  getHasSpu()
}
// 监听三级分类的变化
watch(() => categoryStore.c3Id, () => {
  //如果三级分类id不存在，则退出
  if (!categoryStore.c3Id) return;
  getHasSpu()
})
// 此方法执行可以获取某三级分类下全部spu
const getHasSpu = async (pager = 1) => {
  try {
    if (!pager) {
      pageNo.value = pager
    }
    const res: HasSpuResponseData = await reqHasSpu(pageNo.value, pageSize.value, categoryStore.c3Id)
    if (res.code === 200) {
      total.value = res.data.total
      records.value = res.data.records
    }
  } catch (error: any) {
    ElMessage.error(error)
  }

}
// 页面卸载
onUnmounted(() => {
  // 重置分页
  pageNo.value = 1
  pageSize.value = 5
  // 重置搜索条件
  categoryStore.$reset()
})
// 添加spu
const addSpu = () => {
  scene.value = 1,
    spuForm.value.initAddSpuData(categoryStore.c3Id)
}
// 添加sku
const addSku = (row: SpuData) => {
  scene.value = 2
  // 调用子组件内部的方法初始化添加sku的数据
  skuForm.value.initSkuData(categoryStore.c1Id, categoryStore.c2Id, row)
}
// 修改某条spu
const updateSpu = (row: SpuData) => {
  // 切换为场景1
  scene.value = 1
  // 发请求给子组件获取数据
  spuForm.value.initHasSpuData(row)
}
// 查看某条spu的sku
const findSku = async (row: SpuData) => {
  const res: SkuInfoData = await reqSkuList((row.id) as number)
  // 存储数据
  if (res.code === 200) {
    ElMessage.success("获取成功")
    skuArr.value = res.data
    // 弹出dialog
    dialogVisible.value = true
  } else {
    ElMessage.error(res.message)
  }
}
// 删除某一个spu
const delSpu = async (row: any) => {
  const res = await reqDelSpu(row.id)
  if (res.code === 200) {
    ElMessage.success("删除成功")
    // 重新获取数据
    getHasSpu(records.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage.error(res.message + "请稍后重新获取数据")
  }
}






// 给子组件
const changeScene = (obj: any) => {
  scene.value = obj.flag
  if (obj.params === "update") {
    getHasSpu(pageNo.value)
  } else {
    getHasSpu()
  }
}
</script>

<style scoped></style>
